/**
 * 判断变量是否有值, 若参数未空数组也返回false
 * @param variable
 * @returns {boolean}
 */
export function hasValue(variable) {
  if (variable === null || variable === undefined) {
    return false
  }

  if (variable instanceof Array) {
    return !(variable.length === 0)
  }

  return true
}

/**
 * 判断字符串是否有有效值
 * @param variable
 * @returns {boolean}
 */
export function hasText(variable) {
  return variable !== undefined && variable !== null && variable !== ''
}

/**
 * 去除字符串中的子串
 * @param sourceStr
 * @param subStr
 * @returns {*}
 */
export function removeSubString(sourceStr, subStr) {
  if (!hasText(sourceStr)) {
    return ''
  }

  if (!hasText(subStr)) {
    return sourceStr
  }

  let splitArray = sourceStr.split(subStr)
  return splitArray.join('')
}

/**
 * 去除字符串中多个字串
 * @param sourceStr
 * @param subStrList
 * @returns {*}
 */
export function removeSubStringList(sourceStr, subStrList) {
  if (!hasText(sourceStr)) {
    return ''
  }
  if (!hasValue(subStrList)) {
    return sourceStr
  }

  let resultStr = sourceStr
  for (let i = 0, len = subStrList.length; i < len; i++) {
    resultStr = removeSubString(resultStr, subStrList[i])
  }
  return resultStr
}

/**
 * 深克隆
 * @param sourceObject
 */
export function deepClone(sourceObject) {
  if (!hasValue(sourceObject)) {
    if (sourceObject instanceof Array) {
      return []
    } else {
      return null
    }
  }
  return JSON.parse(JSON.stringify(sourceObject))
}
